<?php
	require_once './Tool/DB/SqlDBManager.class.php';
	require_once './Tool/Common/PrepareInput.php';

	$sqlDBManager= new SqlDBManager();

	$search=prepareInput($_REQUEST['SearchBox']);

	if(!preg_match("/^[0-9A-Za-z]*$/",$search)){
		header('Location: ./AttendanceHome.php?errno=1');
		exit();
	}

	$sql="SELECT ar.NRIC, c.CustomerName, ar.SignInTimeStamp, ar.SignOutTimeStamp, c.Daycare
		FROM attendancerecord ar
		LEFT JOIN Client c ON ar.NRIC = c.NRIC
		WHERE DATE( ar.SignInTimeStamp ) = CURDATE( ) 
		AND (ar.NRIC LIKE  ?
		OR c.CustomerName LIKE  ?
		OR ar.SignInTimeStamp LIKE  ?
		OR ar.SignOutTimeStamp LIKE  ?)";
	
	//Filter the $search
	$search=addslashes($search);
	$search=str_replace("%", "\%", $search);
	$search=str_replace("_", "\_", $search);
	
	$parameters = array("%".$search."%","%".$search."%","%".$search."%","%".$search."%");

	$res=$sqlDBManager->queryRows($sql,$parameters);

	echo"<table class='tablereport' id='tblAttendance' border='0'  valign='top'>
		<tr>
		<td>NRIC</td>
		<td>Name</td>
		<td>Date</td>
		<td>Time In</td>
		<td>Time Out</td>
		<td>Day-Care</td>
		<td>Sign Out<form method='POST'><input type='submit' button class='btn' name='signoutall'value='Sign Out All' size='10' maxlength='9'/></form></td>
		</tr>";

	$count=0;

	if(!empty($res)){
		for($i=0;$i<count($res);$i++){
			echo"<tr><td>".$res[$i]['NRIC']."</td>
				<td>".$res[$i]['CustomerName']."</td>
				<td>".$today = date('j F, Y')."</td>";

			$signin = strtotime($res[$i]['SignInTimeStamp']);
			$signintime = date('H:i:s', $signin);
			echo"<td>".$signintime."</td>";
			if($res[$i]['SignOutTimeStamp']=='0000-00-00 00:00:00'|| $res[$i]['SignOutTimeStamp']==NULL){
				echo"<td></td>
					<td style='text-align:center;vertical-align:middle'>".$res[$i]['Daycare']."</td>
					<td style='text-align:center;vertical-align:middle'>
					<form method='POST' >
					<button type='submit' name='btnSO' value='{$res[$i]['NRIC']}'>Sign Out</button>
					</form>
					</td>";
			}else{ 
				$signout = strtotime($res[$i]['SignOutTimeStamp']);
				$signouttime = date('H:i:s', $signout);
				echo"<td>".$signouttime."</td>
					<td style='text-align:center;vertical-align:middle'>".$res[$i]['Daycare']."</td>
					<td style='text-align:center;vertical-align:middle'></td>";
			}
			echo "</tr>";
			$count++;
		}
	}
	echo "</table><br/><br/>";
	
	//close connection
    $sqlDBManager->close_connect();
?>
